<template>
	<div class="canvas-box" @click="refreshCode">
		<canvas id="code-canvas" :width="contentWidth" :height="contentHeight"></canvas>
	</div>
</template>

<script setup lang="ts" name="identifyCode">
import { onMounted } from "vue";

// 定义组件可传变量
interface identifyCodeProps {
	modelValue: string; // v-model 固定名称
	fontSizeMin?: number; // 字体最小值
	fontSizeMax?: number; // 字体最大值
	backgroundColorMin?: number; // 验证码图片背景色最小值
	backgroundColorMax?: number; // 验证码图片背景色最大值
	dotColorMin?: number; // 背景干扰点最小值
	dotColorMax?: number; // 背景干扰点最大值
	contentWidth?: number; // 容器宽度
	contentHeight?: number; // 容器高度
}
const props = withDefaults(defineProps<identifyCodeProps>(), {
	fontSizeMin: 25,
	fontSizeMax: 35,
	backgroundColorMin: 200,
	backgroundColorMax: 220,
	dotColorMin: 30,
	dotColorMax: 60,
	contentWidth: 132,
	contentHeight: 40,
});

// 定义可触发的事件
const emit = defineEmits(["update:modelValue"]);

const codeList = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; // 验证码字符集
let identifyCode = ""; // 验证码
onMounted(() => refreshCode());

// 刷新验证码
const refreshCode = () => {
	identifyCode = makeCode(codeList, 4);
	drawPic();
};

// 生成验证码
const makeCode = (e: string, n: number) => {
	let icode = "";
	for (let i = 0; i < n; i++) {
		icode += e[randomNum(0, e.length)];
	}
	// console.log("make identifyCode : ", icode);
	emit("update:modelValue", icode);
	return icode;
};

// 生成一个随机数
const randomNum = (min: number, max: number) => {
	return Math.floor(Math.random() * (max - min) + min);
};

// 生成一个随机颜色
const randomColor = (min: number, max: number) => {
	let r = randomNum(min, max);
	let g = randomNum(min, max);
	let b = randomNum(min, max);
	return "rgb(" + r + "," + g + "," + b + ")";
};

// 绘制验证码文本
const drawText = (ctx: CanvasRenderingContext2D, text: string, i: number) => {
	// 随机生成字体颜色
	ctx.fillStyle = randomColor(50, 160);
	// 随机生成字体大小
	ctx.font = randomNum(props.fontSizeMin, props.fontSizeMax) + "px SimHei";
	let x = (i + 1) * (props.contentWidth / (identifyCode.length + 1));
	let y = randomNum(props.fontSizeMax, props.contentHeight - 5);
	let deg = randomNum(-30, 30);
	// 修改坐标原点和旋转角度
	ctx.translate(x, y);
	ctx.rotate((deg * Math.PI) / 180);
	ctx.fillText(text, 0, 0);
	// 恢复坐标原点和旋转角度
	ctx.rotate((-deg * Math.PI) / 180);
	ctx.translate(-x, -y);
};

// 绘制干扰线
const drawLine = (ctx: CanvasRenderingContext2D) => {
	for (let i = 0; i < 4; i++) {
		ctx.strokeStyle = randomColor(100, 200);
		ctx.beginPath();
		ctx.moveTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight));
		ctx.lineTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight));
		ctx.stroke();
	}
};

// 绘制干扰点
const drawDot = (ctx: CanvasRenderingContext2D) => {
	const dotNum = randomNum(props.dotColorMin, props.dotColorMax);
	for (let i = 0; i < dotNum; i++) {
		ctx.fillStyle = randomColor(0, 255);
		ctx.beginPath();
		ctx.arc(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight), 1, 0, 2 * Math.PI);
		ctx.fill();
	}
};

// 绘制验证码图像
const drawPic = () => {
	let canvas = document.getElementById("code-canvas") as HTMLCanvasElement;
	let ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
	// 清除画布
	ctx.clearRect(0, 0, props.contentWidth, props.contentHeight);
	// 绘制背景
	ctx.fillStyle = randomColor(props.backgroundColorMin, props.backgroundColorMax);
	ctx.fillRect(0, 0, props.contentWidth, props.contentHeight);
	// 绘制文字
	ctx.textBaseline = "bottom";
	for (let i = 0; i < identifyCode.length; i++) {
		drawText(ctx, identifyCode[i], i);
	}
	drawLine(ctx);
	drawDot(ctx);
};

// 向外部暴露 refreshCode 方法
defineExpose({ refreshCode });
</script>

<style scoped>
.canvas-box {
	height: 40px;
	overflow: hidden;
	border-radius: 4px;
}
</style>
